/*
 * $Id: ResourceEditionNode.java 488 2008-08-20 16:59:47Z rcleveng $
 * 
 * Copyright (c) 2001,2002 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.ui.internal;

import java.io.InputStream;

import net.sourceforge.perforce.core.resources.RemoteResource;
import net.sourceforge.perforce.ui.PerforceUIPlugin;

import org.eclipse.compare.BufferedContent;
import org.eclipse.compare.CompareUI;
import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.structuremergeviewer.IStructureComparator;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.team.core.TeamException;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pr??mann</a>
 */
public class ResourceEditionNode extends BufferedContent implements IStructureComparator,
    ITypedElement {
  private RemoteResource resource;
  private ResourceEditionNode[] children;

  public ResourceEditionNode(RemoteResource resource) {
    this.resource = resource;
  }

  public Object[] getChildren() {
    if (children == null) {
      if (resource == null) {
        children = new ResourceEditionNode[0];
      } else {
        try {
          RemoteResource[] members = resource.members(new NullProgressMonitor());
          children = new ResourceEditionNode[members.length];
          for (int i = 0, length = members.length; i < length; i++) {
            children[i] = new ResourceEditionNode(members[i]);
          }
        } catch (TeamException e) {
          PerforceUIPlugin.log(e.getStatus());
        }
      }
    }
    return children;
  }

  public Image getImage() {
    return CompareUI.getImage(resource);
  }

  public String getName() {
    return resource == null ? "" : resource.getName(); //$NON-NLS-1$
  }

  public String getType() {
    if (resource == null) {
      return UNKNOWN_TYPE;
    }

    if (resource.isContainer()) {
      return FOLDER_TYPE;
    }

    String name = resource.getName();
    name = name.substring(name.lastIndexOf('.') + 1);
    return name.length() == 0 ? UNKNOWN_TYPE : name;
  }

  @Override
  protected InputStream createStream() throws CoreException {
    if (resource == null) {
      return null;
    }

    try {
      return resource.getContents(new NullProgressMonitor());
    } catch (TeamException e) {
      throw new CoreException(e.getStatus());
    }
  }

  public RemoteResource getRemoteResource() {
    return resource;
  }

  @Override
  public boolean equals(Object other) {
    if (other instanceof ITypedElement) {
      String otherName = ((ITypedElement) other).getName();
      return getName().equals(otherName);
    }

    return super.equals(other);
  }


  @Override
  public int hashCode() {
    return getName().hashCode();
  }
}
